<?php
/*
 * Author:YangBo Created on 2009-1-16:����06:15:38
 * 
 * TODO:TODO
 * as9
 * UserService
 * 
 */
 require_once('./dao/User.php');
 
 require_once('./BaseService.php');
 require_once('./interfaces/IUserService.php');
 require_once("./util/FileSystemUtil.php");
 require_once("./util/ServiceUtil.php");
 class UserService extends BaseService implements IUserService
 {
 	protected $user;
 	
 	public function UserService()
 	{
        $this->initializeDB();
        
        $user = new User();
 	}
 	
 	/**
	 * 创建用户
	 * @param $target [1,n,email,p,171];
	 * @return:success
	*/
 	public function create($target)
 	{
 		$cno = $target['cno'];
 		$name = $target['name'];
 		$email = $target['email'];
 		$password = md5($target['password']);
 		$once_belonged = $target['once_belonged'];
 		$is_admin = $target['is_admin'];
 		
 		try
 		{
 			$this->sql = "SELECT students.email FROM students WHERE email='$email'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			$numRows = mysql_num_rows($this->result);
 			if($numRows>=1)
 			{
 				return ServiceUtil::getFailureResult("failure","email existed!");
 			}else
 			{
 				$this->sql = "SELECT students.name FROM students WHERE name='$name'";
 				$this->result = mysql_query($this->sql,$this->_db);
 				$numRows = mysql_num_rows($this->result);
 				if($numRows>=1)
	 			{
	 				return ServiceUtil::getFailureResult("failure","name existed!");
	 			}else
	 			{
	 				try{
						$this->sql = "INSERT INTO `students` (`cno`, `name`, `email`, `password`,`once_belonged`) VALUES
														 ('$cno', '$name', '$email', '$password', '$once_belonged')";
						$this->result = mysql_query($this->sql,$this->_db);
						try
						{
							$this->sql = "UPDATE class SET class.user_registed=class.user_registed+1 WHERE class.cno='$cno'";
							$this->result = mysql_query($this->sql,$this->_db);
							return ServiceUtil::getSuccessResult("success","create user success!");
						}catch(Exception $exc)
						{
							return $exc;
						}
						
					}catch(Exception $exc)
					{
						return $exc;
					}
	 			}
 			}
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 		
		
											 
	}
 	/**
	 * 读取用户
	 * @param $targetID email
	 * @return:success
	*/
 	public function read($targetID)
 	{
 		if($targetID)
		{
			$email = strtolower(trim($targetID));
	 		try
	 		{
	 			$this->sql = "SELECT * FROM students WHERE email='$email'";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			while($row = mysql_fetch_object($this->result))
		 		{
		 			$this->user->sno = $row->sno;
		 			$this->user->cno = $row->cno;
		 			$this->user->name = $row->name;
		 			$this->user->password = $row->password;
		 			$this->user->email = $row->email;
		 			$this->user->once_belonged = $row->once_belonged;
		 			$this->user->is_admin = $row->is_admin;
		 			$this->user->is_super_admin = $row->is_super_admin;
		 			$this->user->registed_time = $row->registed_time;
		 			$this->user->capcity_upload = $row->capcity_upload;
		 			$this->user->uploaded = $row->uploaded;
		 			$this->user->capcity_message = $row->capcity_message;
		 			$this->user->messaged = $row->messaged;
		 			
		 			$this->ret[] = $this->user;
		 		}
		 		return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
		}else
		{
	 		try
	 		{
	 			$this->sql = "SELECT * FROM students";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			$numRows = mysql_num_rows($this->result);
	 			for($i=0;$i<$numRows;$i++)
	 			{
	 				$row = mysql_fetch_object($this->result);
	 				$this->user = new User();
	 				$this->user->sno = $row->sno;
		 			$this->user->cno = $row->cno;
		 			$this->user->name = $row->name;
		 			$this->user->password = $row->password;
		 			$this->user->email = $row->email;
		 			$this->user->once_belonged = $row->once_belonged;
		 			$this->user->is_admin = $row->is_admin;
		 			$this->user->is_super_admin = $row->is_super_admin;
		 			$this->user->registed_time = $row->registed_time;
		 			$this->user->capcity_upload = $row->capcity_upload;
		 			$this->user->uploaded = $row->uploaded;
		 			$this->user->capcity_message = $row->capcity_message;
		 			$this->user->messaged = $row->messaged;
		 			
		 			$this->ret[] = $this->user;
	 			}
		 		return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
		}
 		
 	}
 	/**
	 * 更新用户
	 * @param $target [1,n,u,p,email,171];
	 * @return:success
	*/
 	public function update($target)
 	{
 		$sno = $target['sno'];
 		$cno = $target['cno'];
 		$name = $target['name'];
 		$email = $target['email'];
 		$password = md5($target['password']);
 		$once_belonged = $target['once_belonged'];
 		$is_admin = $target['is_admin'];
 		
 		try
 		{
 			$this->sql = "UPDATE students SET cno='$cno',name='$name',password='$password',email='$email',once_belonged='$once_belonged' WHERE sno='$sno'";
 			$this->reuslt = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","update student success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 删除用户
	 * @param $targetID sno
	 * @return:success
	*/
 	public function delete($targetID)
 	{
 		$sno = strtolower(trim($targetID));
 		if($sno)
 		{
 			try
	 		{
	 			$this->sql = "DELETE FROM students WHERE sno='$sno'";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			return ServiceUtil::getSuccessResult("success","delete student success!");
	 		}catch(Exception $exc)
	 		{
				return $exc; 			
	 		}
 		}
 	}
 	
 	/**
	 * 尝试登录
	 * @param $simpleUser ["email","password"]
	 * @return:success
	*/
 	public function tryLogin($simpleUser)
 	{
 		$email = strtolower(trim($simpleUser[0]));
 		$password = md5(strtolower(trim($simpleUser[1])));
// 		$email = $simpleUser['email'];
// 		$password = $simpleUser['password'];
 		
 		try
 		{
 			$this->sql = "SELECT students.email,students.password FROM students WHERE email='$email' AND password='$password'";
 			$this->result = mysql_query($this->sql,$this->_db);
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 		
 		if($this->result)
 		{
 			$resultRows=mysql_num_rows($this->result);
 			if($resultRows>=1)
 			{
 				return ServiceUtil::getSuccessResult("success","login success!");
 			}else
 			{
 				return ServiceUtil::getFailureResult("failure","login failure!");;
 			}
 		}else
 		{
 			return trigger_error("AMFPHP Remoting 'UserService_tryLogin' class database Check query error: ");
 		}
 	}
 	
 	public function getUploadToken($userID)
 	{
// 		FileSystemUtil::createDir("test1");
 		return "getUploadToken!";
 	}
 	/**
	 * 上传相片数
	 * @param $sName sName
	 * @return:int
	*/
 	public function getUploadsAmount($sName)
 	{
 		$mySname = strtolower(trim($sName));
 		try
 		{
 			$this->sql = "SELECT photos.uploader FROM photos WHERE uploader='$mySname'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			$numRows = mysql_num_rows($this->result);
 			return $numRows;
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 相片留言数
	 * @param $sName sName
	 * @return:int
	*/
 	public function getMessagesAmount($sName)
 	{
 		$mySname = strtolower(trim($sName));
 		try
 		{
 			$this->sql = "SELECT messages.messager FROM messages WHERE messager='$mySname'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			$numRows = mysql_num_rows($this->result);
 			return $numRows;
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	
 	public function __destruct()
 	{
 		mysql_free_result($this->_db);
 	}
 }
?>
